package com.zshan.clinic.web.listener;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zshan.clinic.admin.constant.RedisKeyPrefix;
import com.zshan.clinic.admin.entity.medical.CallLargeMessage;
import com.zshan.clinic.common.util.json.JsonUtil;
import com.zshan.clinic.web.netty.enums.ProtocolTypeEnum;
import com.zshan.clinic.web.netty.manager.ChannelManager;
import com.zshan.clinic.web.netty.model.WsMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author lichen
 */
@Component
@Slf4j
public class RedisMessageSubscriber implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String topic = new String(message.getChannel());
        String body = new String(message.getBody());
        log.info("接收到消息topic:{},内容:{}",topic,body);
        if(RedisKeyPrefix.TOPIC_CLINIC_CALL_LARGE.equals(topic)){
            //大屏叫号
            CallLargeMessage callLargeMessage = JsonUtil.json2Obj(body,new TypeReference<CallLargeMessage>() {});
            WsMessage wsMessage = new WsMessage();
            wsMessage.setCode(ProtocolTypeEnum.BIG_CALL.getValue());
            wsMessage.setData(callLargeMessage);
            wsMessage.setMsg(ProtocolTypeEnum.BIG_CALL.getText());
            ChannelManager.sendToClinic(callLargeMessage.getClinicId(),JsonUtil.obj2Json(wsMessage));
        }
    }
}

